﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ninject;
using SomeRandomGame.Interfaces;
using SomeRandomGame.Utility;

namespace SomeRandomGame.Screens
{
    public class LoadingScreen: GameScreen
    {
        private bool _loadingIsSlow;
        private bool _otherScreensAreGone;
        private IList<GameScreen> _screensToLoad;
        private IGameManager _gameManager;

        private LoadingScreen( IGameManager gameManager, bool loadingIsSlow, IList<GameScreen> screensToLoad )
        {
            _loadingIsSlow = loadingIsSlow;
            _screensToLoad = screensToLoad;
            _gameManager = gameManager;

            TransitioningInTime = TimeSpan.FromSeconds( 0.5 );
        }

        public static void Load( IGameManager gameManager, bool loadingIsSlow, PlayerIndex? controllingPlayer, params GameScreen[] screensToLoad )
        {
            var screenManager = gameManager.ServiceLocator.Get<IScreenManager>();

            foreach( GameScreen screen in screenManager.Screens )
            {
                screen.ExitScreen();
            }

            LoadingScreen loadingScreen = new LoadingScreen( gameManager, loadingIsSlow, screensToLoad );

            screenManager.AddScreen( gameManager, loadingScreen, controllingPlayer );
        }

        public override void Update( GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen )
        {
            base.Update( gameTime, otherScreenHasFocus, coveredByOtherScreen );

            if( _otherScreensAreGone )
            {
                ScreenManager.RemoveScreen( this );

                foreach( GameScreen screen in _screensToLoad )
                {
                    if( screen != null )
                    {
                        ScreenManager.AddScreen( _gameManager, screen, ControllingPlayer );
                    }
                }

                // Once the load has finished, we use ResetElapsedTime to tell
                // the  game timing mechanism that we have just finished a very
                // long frame, and that it should not try to catch up.
                _gameManager.Game.ResetElapsedTime();
            }
        }

        public override void Draw( SpriteBatch spriteBatch, IGameManager gameManager, GameTime gameTime )
        {
            if( ScreenState == ScreenState.Active && ScreenManager.Screens.Count == 1 )
            {
                _otherScreensAreGone = true;
            }

            if( _loadingIsSlow )
            {
                SpriteFont font = _gameManager.ServiceLocator.Get<IContentFactory>().GameFont;

                // Center the text in the viewport.
                Vector2 viewportSize = new Vector2( gameManager.Game.GraphicsDevice.Viewport.Width, gameManager.Game.GraphicsDevice.Viewport.Height );
                Vector2 textSize = font.MeasureString( Constants.Strings.LOADING );
                Vector2 textPosition = (viewportSize - textSize) / 2;

                spriteBatch.Begin();
                spriteBatch.DrawString( font, Constants.Strings.LOADING, textPosition, Color.White * TransitionAlpha );
                spriteBatch.End();
            }
        }
    }
}
